/**
 * @(#) SchoolExpMediator.as
 *
 * @date		2010. 11. 29
 * @version		1.0
 * @author		Shin, Yunwoo
 * @client		F&amp;U
 * @module		SchoolExp(학자금 신청)
 * @description	
 */
package com.skt.common_hr.schoolexp.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.schoolexp.model.SchoolExpListProxy;
	import com.skt.common_hr.schoolexp.model.SchoolExpProxy;
	import com.skt.common_hr.schoolexp.view.components.SchoolExpView;
	
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	
	import mx.collections.Sort;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class SchoolExpMediator extends OverViewMediator
	{
		public static const NAME:String = "SchoolExpMediator"; 

		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function SchoolExpMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
 			super.initializeView();
 			setEventListeners();
            preloadData();
 		}
 		
 		override protected function activatedView():void
 		{
 			super.activatedView();
 			view.changeMode(SchoolExpView.MODE_OVERVIEW);
 			loadSchoolExpList(view.selected_year);
 		}
 		
 		override protected function closedView():void
 		{
 			super.closedView();
 			onCloseReqView();
 			removeEventListeners();
 		}
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(SchoolExpView.CHANGED_YEAR, onChangedYear);
 			view.addEventListener(SchoolExpView.CLICK_REQUEST, onClickRequest);
 			view.addEventListener(SchoolExpView.MODIFY_REQUEST, onClickModify);
 			view.addEventListener(SchoolExpView.DELETE_REQUEST, onClickDelete);
 			view.addEventListener(SchoolExpView.PRINT_REQUEST, onClickPrint);
 			view.addEventListener(SchoolExpView.READONLY_REQUEST, onClickRead);
 		}
 		
 		private function removeEventListeners():void
 		{
 			view.removeEventListener(SchoolExpView.CHANGED_YEAR, onChangedYear);
 			view.removeEventListener(SchoolExpView.CLICK_REQUEST, onClickRequest);
 			view.removeEventListener(SchoolExpView.MODIFY_REQUEST, onClickModify);
 			view.removeEventListener(SchoolExpView.DELETE_REQUEST, onClickDelete);
 			view.removeEventListener(SchoolExpView.PRINT_REQUEST, onClickPrint);
 			view.removeEventListener(SchoolExpView.READONLY_REQUEST, onClickRead);
 		}
 		
 		private function preloadData():void
		{
			//var yearProxy:WelfareYearProxy = facade.retrieveProxy(WelfareYearProxy.NAME) as WelfareYearProxy;
			//yearProxy.load(Variables.myPernr);
		}
 		
 		/****
 		 * 리스트 데이터 조회
 		 * **/
 		private function loadSchoolExpList(year:String):void
 		{
	 		var proxy:SchoolExpListProxy = facade.retrieveProxy(SchoolExpListProxy.NAME) as SchoolExpListProxy;
 			proxy.load(Variables.myPernr, year);
 		}
 		
 		
  		private function onChangedYear(e:Event):void
 		{
 			loadSchoolExpList(view.selected_year);
 		}
 		
		private function onClickRequest(e:Event):void
 		{
 			executeReqView()
 		}
 		
		private function onClickModify(e:Event):void
 		{
 			executeModifyView(view.current_request.WEL_KEY01);
 		}
 		
		private function onClickRead(e:Event):void
 		{
 			executeReadOnlyView(view.current_request.WEL_KEY01);
 		}
 		
		private function onClickDelete(e:Event):void
 		{
 			CommonAlert.showMsgYesNo("", "선택하신 데이터를 삭제하시겠습니까?", null, "확인", view, deleteData, noAction);
 		}
 		
		private function deleteData():void
 		{
 			var proxy:SchoolExpProxy = facade.retrieveProxy(SchoolExpProxy.NAME) as SchoolExpProxy;
 			proxy.deleteData(view.current_request.WEL_KEY01, Variables.myPernr);
 		}

		private function noAction():void
		{
			
		}
		private function onClickPrint(e:Event):void
 		{
 			var proxy:SchoolExpProxy = facade.retrieveProxy(SchoolExpProxy.NAME) as SchoolExpProxy;
 			proxy.printData(view.current_request.WEL_KEY01);
 			
 		}
 		

 		/**
		 * 경조사 신규 신청 및 수정 화면 실행
		 * @param
		 * @return void
		 */
 		private function executeReqView(param:Object=null):void
 		{
 			commonFacade.setStatusMessage("");
 			view.changeMode(SchoolExpView.MODE_REQUEST);
			
 			var reqMediator:ReqSchoolExpMediator = facade.retrieveMediator(ReqSchoolExpMediator.NAME) as ReqSchoolExpMediator;
 			
 			if(!reqMediator) {
				reqMediator = new ReqSchoolExpMediator(view.reqView);
	 			facade.registerMediator(reqMediator);
			}
 			if(reqMediator) facade.sendNotification(ReqSchoolExpMediator.INIT_REQ_SCHOOLEXP, param);
 		}
 		
 		/**
		 * 경조사 신규 신청 및 수정 화면 실행
		 * @param
		 * @return void
		 */
 		private function executeModifyView(param:Object=null):void
 		{
 			commonFacade.setStatusMessage("");
 			view.changeMode(SchoolExpView.MODE_REQUEST);
			
 			var reqMediator:ReqSchoolExpMediator = facade.retrieveMediator(ReqSchoolExpMediator.NAME) as ReqSchoolExpMediator;
 			
 			if(!reqMediator) {
				reqMediator = new ReqSchoolExpMediator(view.reqView);
	 			facade.registerMediator(reqMediator);
			}
 			if(reqMediator) facade.sendNotification(ReqSchoolExpMediator.MODIFY_REQ_SCHOOLEXP, param);
 		}
 		
 		/**
		 * 경조사 신규 신청 및 수정 화면 실행
		 * @param
		 * @return void
		 */
 		private function executeReadOnlyView(param:Object=null):void
 		{
 			commonFacade.setStatusMessage("");
 			view.changeMode(SchoolExpView.MODE_REQUEST);
			
 			var reqMediator:ReqSchoolExpMediator = facade.retrieveMediator(ReqSchoolExpMediator.NAME) as ReqSchoolExpMediator;
 			
 			if(!reqMediator) {
				reqMediator = new ReqSchoolExpMediator(view.reqView);
	 			facade.registerMediator(reqMediator);
			}
 			if(reqMediator) facade.sendNotification(ReqSchoolExpMediator.READONLY_REQ_SCHOOLEXP, param);
 		}
 		
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat( [
					SchoolExpListProxy.LOAD_SCHOOLEXP_LIST_COMPLETE,
					SchoolExpListProxy.LOAD_SCHOOLEXP_LIST_FAILED,
					SchoolExpProxy.DELETE_SCHOOLEXP_COMPLETE,
					SchoolExpProxy.DELETE_SCHOOLEXP_FAILED,
					SchoolExpProxy.LOAD_PRINTDATA_COMPLETE,
					SchoolExpProxy.LOAD_PRINTDATA_FAILED,
					ReqSchoolExpMediator.CLOSED_REQUEST_VIEW
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case SchoolExpListProxy.LOAD_SCHOOLEXP_LIST_COMPLETE:
					onLoadSchoolExpList(note.getBody() as SchoolExpListProxy);
					break;
				case ReqSchoolExpMediator.CLOSED_REQUEST_VIEW:
					onCloseReqView();
					break;
				case SchoolExpProxy.LOAD_PRINTDATA_COMPLETE:
					onPrintData(note.getBody() as SchoolExpProxy);
					break;
				case SchoolExpProxy.DELETE_SCHOOLEXP_COMPLETE:
					CommonAlert.showMessage("", "성공적으로 삭제되었습니다.");
					loadSchoolExpList(view.selected_year);
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
		private function onLoadSchoolExpList(proxy:SchoolExpListProxy):void
		{
			var sort:Sort = new Sort();
			sort.compareFunction = compareList;
			
			view.dataProvider = proxy.schoolExpList;
			view.dataProvider.sort = sort;
			view.dataProvider.refresh();
		}
		
		private function compareList(a:Object, b:Object, fields:Array = null):int
		{
			var status:String = "32145";
			
			if(a.WSTATUS==b.WSTATUS) {	// 결재상태가 같으면
				if(Number(a.BEGDA) < Number(b.BEGDA)) {
					return 1;
				}
				if(Number(a.BEGDA) > Number(b.BEGDA)) {
					return -1;
				}
				return 0;
			}

			if(status.indexOf(a.WSTATUS) < status.indexOf(b.WSTATUS)) {
				return 1;
			}
			return -1;
		}
		
		private function onPrintData(proxy:SchoolExpProxy):void
		{
			openNavigateToURL("SchoolExpReport.jsp", proxy.urlParam);
		}
		/**
		 * Main Module에서 QueryClose 메세지가 왔을 때의 처리
		 *     - 사용자에게 확인하고 View에 종료처리 -> MainModule에 'CLOSE_MODULE' 전송
		 * @param
		 * @return void
		 */
		 /*
		private function onQueryCloseModule():void
		{
			AlertHelper.showMsgYesNo("Close?", "확인", view, onMsgQueryCloseY, onMsgQueryCloseN);
		}
		*/
 
 		private function onCloseReqView():void
 		{
 			view.changeMode(SchoolExpView.MODE_OVERVIEW);
 			loadSchoolExpList(view.selected_year);
 		}
 
 
 		/**
		 * 프린트상세정보
		 * */
		private function onPrintDetailLoad(proxy:SchoolExpProxy):void
		{
			//view.printView.printList = proxy.printList;
			//view.printView.bankInfoVo = proxy.printMaster;
			//view.printView.totalMoney = NumberUtil.NumberFormat(proxy.totMoney);
			openNavigateToURL("ReqSchoolExpReport.jsp", proxy.urlParam);
		}
		
 		public function openNavigateToURL(url:String, params:URLVariables):void {	
			var option:String = "toolbar=no,menubar=no" +
					", location=no, directions=no" 	+ 
					", scrollbars=yes, status=no"	+
					", width=300" +
					", height=760";

			ExternalInterface.call("execPrintWindow");
			//System.useCodePage = true;

        	var request:URLRequest = new URLRequest(url);
			//var request:URLRequest = new URLRequest("javascript:window.open('"+url+"',"+option+"');");

        	request.method = URLRequestMethod.POST;
        	request.data  = params;

			// System.useCodePage = false;

   			navigateToURL(request, "eHR_print");
		}


 
 
 
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////




		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():SchoolExpView
        {
            return viewComponent as SchoolExpView;
        }
		
		
	}
}